﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShenLanCMS.Models;

namespace ShenLanCMS.Web.Controllers
{
    public class AdTypeController : Controller
    {
        private EntityContext db = new EntityContext();

        [IsAdmin]
        public ViewResult Index()
        {
            return View(db.AdTypes.ToList());
        }

        [IsAdmin]
        public ActionResult Create()
        {
            return View();
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Create(AdType adtype)
        {
            if (ModelState.IsValid)
            {
                foreach (var item in db.AdTypes)
                {
                    if (item.TypeName == adtype.TypeName)
                    {
                        ModelState.AddModelError("typeName", "该类型已经存在");
                        return View(adtype);
                    }
                }
                adtype.AdTypeID = Guid.NewGuid();
                db.AdTypes.Add(adtype);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(adtype);
        }

        [IsAdmin]
        public ActionResult Edit(Guid id)
        {
            AdType adtype = db.AdTypes.Find(id);
            return View(adtype);
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Edit(AdType adtype)
        {
            if (ModelState.IsValid)
            {
                foreach (var item in db.AdTypes)
                {
                    if (item.TypeName == adtype.TypeName && item.AdTypeID != adtype.AdTypeID)
                    {
                        ModelState.AddModelError("typeName", "该类型已经存在");
                        return View(adtype);
                    }
                }
                var sql = string.Format("update adtypes set typename='{0}' where adtypeid='{1}'", adtype.TypeName, adtype.AdTypeID);
                var n = db.Database.ExecuteSqlCommand(sql);
                if (n > 0)
                    return RedirectToAction("Index");
                else
                {
                    ModelState.AddModelError("typeName", "保存失败");
                    return View(adtype);
                }
            }
            return View(adtype);
        }

        [IsAdmin]
        public ActionResult Delete(Guid id)
        {
            try
            {
                var ads = db.Ads.Where(n => n.AdTypeID == id);
                if (ads.Count() > 0)
                    return Json("该类型正在使用中不能删除");
                AdType adtype = db.AdTypes.Find(id);
                db.AdTypes.Remove(adtype);
                db.SaveChanges();
                return Json("删除成功");
            }
            catch { return Json("删除失败"); }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}